// elsePage/pages/pinTuanDingDan/pinTuanDingDan.js
const util = require('../../../utils/util.js')

Page({
  data: {
    imgUrls: getApp().globalData.imgUrls,
    activityList: ['拼团订单', '限时秒杀', '砍价'],
    idx: 0,
    page: 1,
    showNum: 3,
    loading: false,
    list: []
  },

  choose(e) {
    let index = e.currentTarget.dataset.index,
      idx = this.data.idx
    if (idx != index) {
      this.setData({
        list: [],
        idx: index,
        page: 1
      })
      this.getData()
    }
  },
  onLoad(options) {

  },

  onShow: function() {
    this.setData({
      list: [],
      page: 1
    })
    this.getData()
  },
  jumpDdxq(e) { //点击查看订单详情
    let distributiontype = e.currentTarget.dataset.distributiontype,
      status = e.currentTarget.dataset.status,
      order_id = e.currentTarget.dataset.orderid;

 
    if (status == 2) { // 订单已经取消
      wx.navigateTo({
        url: `/pages/DingDanXiangQingQuXiao/DingDanXiangQingQuXiao?order_id=${order_id}&distributiontype=${distributiontype}`,
      })
    } else {
      if (distributiontype == 1) { //商家配送
        if (status == 1) { // 商家配送--待发货
          wx.navigateTo({
            url: `/pages/DingDanXiangDaiFaHuo/DingDanXiangDaiFaHuo?order_id=${order_id}&distributiontype=${distributiontype}`
          })
        } else if (status == 3) { // 同城限时达--待收货
          wx.navigateTo({
            url: `/pages/DingDanXiangDaiShouHuo/DingDanXiangDaiShouHuo?order_id=${order_id}&distributiontype=${distributiontype}`
          })
        } else if (status == 5) { // 订单完成
          wx.navigateTo({
            url: `/pages/DingDanXiangYiWanChengSJPS/DingDanXiangYiWanChengSJPS?order_id=${order_id}&distributiontype=${distributiontype}`
          })
        }
      } else if (distributiontype == 2) { // 同城限时达
        if (status == 1) { // 同城限时达--待发货
          wx.navigateTo({
            url: `/pages/DingDanXiangDaiFaHuo/DingDanXiangDaiFaHuo?order_id=${orderid}&distributiontype=${distributiontype}`
          })
        } else if (status == 3) { // 同城限时达--待收货
          wx.navigateTo({
            url: `/pages/DingDanXiangDaiShouHuo/DingDanXiangDaiShouHuo?order_id=${orderid}&distributiontype=${distributiontype}`
          })
        } else if (status == 5) { // 订单完成
          wx.navigateTo({
            url: `/pages/DingDanXiangYiWanChengSJPS/DingDanXiangYiWanChengSJPS?order_id=${orderid}&distributiontype=${distributiontype}`
          })
        }
      } else if (distributiontype == 3) { // 到店自提
        if (status == 1) { // 到店自提--待发货
          wx.navigateTo({
            url: `/pages/DingDanXiangBeiHuo/DingDanXiangBeiHuo?order_id=${orderid}&distributiontype=${distributiontype}`
          })
        }
        if (status == 3) { // 到店自提--待收货
          wx.navigateTo({
            url: `/pages/DingDanXiangYiWanCheng/DingDanXiangYiWanCheng?order_id=${dataset.orderid}&distributiontype=${distributiontype}&heXiao=false`
          })
        }
        if (status == 5) { // 到店自提--完成
          wx.navigateTo({
            url: `/pages/DingDanXiangYiWanCheng/DingDanXiangYiWanCheng?order_id=${dataset.orderid}&distributiontype=${distributiontype}&heXiao=true`
          })
        }
      }
    }




  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.getData()
  },
  jump(e) { // 点击去支付
    let market_type_id = e.currentTarget.dataset.market_type_id,
      activity_id = e.currentTarget.dataset.activity_id,
      order_id = e.currentTarget.dataset.order_id,
      market_type = e.currentTarget.dataset.market_type;
    wx.navigateTo({
      url: `/pages/Dingdan/Dingdan?market_type_id=${market_type_id}&order_id=${order_id}&activity_id=${activity_id}&market_type=${market_type}`,
    })
  },
  jumpPTXQ(e) { // 点击查看订单详情  未支付
    let order_id = e.currentTarget.dataset.order_id
    wx.navigateTo({
      url: `/elsePage/pages/pinTuanXianQing/pinTuanXianQing?order_id=${order_id}&fromList=true`
    })
  },
  jumpXQ(e) { // 点击再次购买
    let goods = e.currentTarget.dataset.goods
    goods = goods.map((item) => {
      let obj = {
        "goods_id": item.goods_id,
        "goods_stock": 1,
        "sku": 215,
        "uniqueKey": "68775d26b16d43bab128"
      }
      return obj
    })
    // wx.navigateTo({
    //   url: `../DingdanPay/DingdanPay?goods=${e.currentTarget.dataset.goodsid}`,
    // })
  },
  cancellationOfOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: e.currentTarget.dataset.status == 'shanchu' ? '是否确定删除订单' : '是否确定取消订单',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Cancellation_order', {
            order_id: e.currentTarget.dataset.id
          }, 'application/x-www-form-urlencoded').then(res => {
            if (res.code == 200) {
              let list = that.data.list,
                index = e.currentTarget.dataset.idx
              list.splice(index, 1)
              that.setData({
                list
              })
              wx.showToast({
                title: '删除成功',
                success() {
                  that.getData()
                }
              })
            } else {
              wx.showToast({
                title: res.msg + '',
                icon: 'none'

              })
            }

          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goRefund() { // 退款订单
    wx.navigateTo({
      url: '/pages/DingDanTuiKuan/DingDanTuiKuan',
    })
  },
  receipt(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Confirmation_receipt', {
            order_id: e.currentTarget.dataset.orderid
          }, 'application/x-www-form-urlencoded').then(res => {
            wx.showToast({
              title: '收货成功',
              duration: 1000
            })
            setTimeout(() => {
              that.setData({
                list: []
              })
              that.getData()
            }, 1000)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  getLogistics(e) { //点击查看物流
    wx.navigateTo({
      url: `/pages/Logistics/Logistics?order_id=${e.currentTarget.dataset.id}`,
    })
  },
  getData() {
    util.API('pyapi/activity_order_list', {
      page: this.data.page,
      pageshow: 3,
      market_type: parseInt(this.data.idx) + 1
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        all_orders_info,
        code,
        count,
        msg
      } = res
      if (code == 200) {
        all_orders_info.forEach(item => {
          item.activity.endTime = util.getTimeDian(item.activity.etime)
          item.activity.startTime = util.getTimeDian(item.activity.stime)
          item.goods_list.forEach(items => { // tupian
            items.cover = items.cover.split(',')[0]
          })
          item.size = this.specifications(item.goods_list)
        })

        this.setData({
          list: this.data.list.concat(all_orders_info)
        })
      } else {
        this.setData({
          loading: true,
          page: this.data.page - 1
        })
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  // jump(e) {
  //   if (e.currentTarget.dataset.class == 'pay') {
  //     wx.navigateTo({
  //       url: `../Dingdan/Dingdan?order_id=${e.currentTarget.dataset.orderid}`,
  //     })
  //   }
  // },
  gopay(e) {
    if (e.currentTarget.dataset.status == 'pinglun') {
      wx.navigateTo({
        url: `../Shaidan/Shaidan?order_id=${e.currentTarget.dataset.orderid}`,
      })
    }
  },
  specifications(trg) { //商品规格转换
    let len = trg.length
    for (let i = 0; i < len; i++) {
      let size = ''
      let str = `${trg[i].sku_type}`
      trg[i].sku_type = JSON.parse(str)
      for (let j = 0; j < trg[i].sku_type.length; j++) {
        for (let prop in trg[i].sku_type[j]) {
          size += trg[i].sku_type[j][prop] + " / "
        }
      }
      trg[i].size = size
      trg[i].size = trg[i].size.substring(0, trg[i].size.length - 3)
    }
    return trg
  }
})